package controller;


import java.util.List;
import model.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserServiceI;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/7.
 */
@Controller
public class LoginController {
    private static final Logger log = Logger.getLogger(LoginController.class);
    @Resource
    private UserServiceI userServiceI;

    /**
     * 用户登陆验证
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login" ,method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Map<String,Object> login(String username,String password,HttpServletRequest request){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User u = userServiceI.findUser(user);
        HttpSession session = request.getSession();
        Map<String,Object> map = new HashMap<String, Object>();
        if(null != u){
            session.setAttribute("user",u);
            map.put("data", "success");
        }else{
            map.put("data","fail");
        }
        return map;
    }

    /**
     * 跳转到主页面
     * @return
     */
    @RequestMapping(value = "main")
    public String toMain(){
        return "index";
    }



}
